/**************************************************
创建机构类
* */
<template>
  <div class="mod-config">
    <basic-container>
      <!--<navigationZhuanX :sign="{a:true,b:false,c:false,d:false,e:false}"></navigationZhuanX>-->
      <navigationSJKH :sign="{ a: true, b: false, c: false,d: false,e: false}"></navigationSJKH>
      <div v-if="showZhong==1" class="boxsty zhiBiao">
        <div :height="searchheight">
          <span
            style="float:right;margin-top: -30px;cursor: pointer;"
            @click="searchchangeadvanced"
          >
            {{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
            <i
              :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
            />
          </span>
          <div v-show="searchadvanced">
            <el-form
              :inline="true"
              :model="searchForm"
              @keyup.enter.native="getDataList()"
              class="form-inline"
            >
              <el-form-item label="分类">
                <el-input v-model="searchForm.fl" placeholder="分类" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item label="依据">
                <el-input v-model="searchForm.yj" placeholder="依据" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item>


                <el-button
                  v-if="permissions.rckhsjyjk_view"
                  icon="el-icon-search"
                  type="primary"
                  class="marginLeft"
                  @click="getDataList()"
                >检索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div class="mt10" style="overflow:hidden">
          <div style="display:flex;justify-content:space-between">
            <div>

              <el-button
                v-if="permissions.rckhsjyjk_add"
                type="primary"
                @click="addOrUpdateHandle()"
              >新增</el-button>
              <el-button
                type="primary"
                @click="exportXlsx"
              >模板下载</el-button>
              <el-button
                type="primary"
                @click="openImport"
              >导入</el-button>
              <el-button
                v-if="permissions.rckhsjyjk_del"
                type="primary"
                @click="deleteTabData"
              >删除</el-button>

             <!-- <el-tooltip class="item" effect="dark" content="下载《专项工作使用说明书》" placement="top-start">
                <el-link icon="el-icon-download" @click="exportXlsx" type="primary" >使用说明</el-link>
              </el-tooltip>-->

            </div>


            <div>
             <!-- <el-popover
                placement="top-start"
                width="200"
                trigger="hover"
                style="margin:0 5px 0 5px"
                content="审核关系和评价关系后才可提交审核。"
              >-->
             <!-- <el-tooltip class="item" effect="dark" content="完成审核关系和评分关系后才能够提交" placement="top-start">
                <div>
                  <el-button
                    v-if="permissions.zxgz_zxgzpro_sub"
                    :disabled="canSubmit"
                    type="primary"
                    @click="goSubmitEvaluateList"
                  >提交审核</el-button>
                </div>
              </el-tooltip>-->
             <!-- </el-popover>-->
            </div>
          </div>
        </div>
        <el-table
          style="width: 100%;margin-top:20px"
          :data="dataList"
          border
          header-align="center"
          v-loading="dataListLoading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column type="index" :index="indexMethod" width="80" align="center" label="序号"></el-table-column>
          <el-table-column prop="fl" align="center" label="分类" show-overflow-tooltip></el-table-column>
          <el-table-column prop="yj" align="center" min-width="100" label="依据"  show-overflow-tooltip></el-table-column>
          <el-table-column prop="zbgz" align="center" min-width="100" label="指标规范"  show-overflow-tooltip>
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <span v-if="scope.row.zbgz == 0">正指标</span>
                <span v-else-if="scope.row.zbgz == 1">负指标</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="jkfbz" align="center" min-width="100" label="加扣分标准"  show-overflow-tooltip></el-table-column>
          <el-table-column prop="yjkfl" align="center" min-width="100" label="依据库分类"  show-overflow-tooltip>
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <span v-if="scope.row.yjkfl == 'ry' ">人员库</span>
                <span v-else-if="scope.row.yjkfl == 'jg'">机构库</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" align="center" min-width="120" label="创建时间"  show-overflow-tooltip></el-table-column>
          <el-table-column align="left" width="240" fixed="right" label="操作">

            <template slot-scope="scope">
              <el-button
                v-if="permissions.rckhsjyjk_view"
                type="primary"
                plain
                size="small"
                @click="dataView(scope.row)"
              >查看</el-button>
                <el-button
                  v-if="permissions.rckhsjyjk_edit"
                  type="primary"
                  plain
                  size="small"
                  @click="addOrUpdateHandle(scope.row)"
                >修改</el-button>


            </template>
          </el-table-column>
        </el-table>
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            background
            layout="total, sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
      <el-dialog width="40%" title="导入" :visible.sync="innerVisible" append-to-body>
        <el-row class="mb10" style="height: 10vh;" :gutter="10">
          <el-col :span="4">
            <el-upload
              style="margin-left: 8px;"
              class="upload-demo"
              action
              :on-change="handleChange"
              :show-file-list="false"
              :on-remove="handleRemove"
              :file-list="fileListUpload"
              :limit="limitUpload"
              accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
              :auto-upload="false"
            >
              <el-button type="primary" plain class="fr">导入</el-button>
            </el-upload>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 弹窗, 新增 / 修改 -->
      <addAndUpdate
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        :dialogTitle="dialogTitle"
        @refreshDataList="getDataList"
        @refreash="refreashdata"
        @closeDialog="closeDialog"
      ></addAndUpdate>
      <detailView v-if="detailVisible" ref="detailNode" @closeDialog="closeDialog1"></detailView>

    </basic-container>
  </div>
</template>

<script>
import addAndUpdate from "@/views/dailyExamine/eventExamine/examineDepot/organDepot/addAndUpdate";

import detailView from "@/views/dailyExamine/eventExamine/examineDepot/organDepot/detailView";

import { changeAss } from "@/api/hetongrenwu/hetonghtxxdjb";

// 接口 api
import {
  userList,
  deleteChoose,
  dataDetail,
  addUserImport
} from "@/api/dailyExamine/eventExamine/event.js";
import { mapGetters, mapState } from "vuex";
import { showMessage } from "@/api/activiti/task";
import store from "@/store";
export default {
  components: {
    detailView,
    addAndUpdate,
    changeAss
  },
  data() {
    return {
      headers: {
        Authorization: "Bearer " + store.getters.access_token
      },
      detailVisible: false,
      searchForm: {}, //查询表单
      canDelete: true, // 删除表格数据的禁用
      multipleSelection: [], //表格中被选中的数据
      dialogTitle: "新增",
      showZhong: 1,
      dataList: [],
      //new
      hisTableData: [],
      actPicUrlList: [],
      actPicUrl: "",
      showPicDialog: false,
      usercommentTask: false,
      goSpVisible: false,


      dataListLoading: false,
      addOrUpdateVisible: false,
      searchadvanced: true,
      searchheight: "0",
      zxshformref: {},
      zxid: Number,
      /*pageIndex: null,
      pageSize: null,
      totalPage: null,
      //new----------
      page: {
        currentPage: 1,
        pageSize: 10
      },*/
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,

      evaluationList: [],
      canSubmit: true,
      showLd:'',
      innerVisible: false,
      dialogVisible: false,
      fileListUpload: [],
      limitUpload: 3,
      accountList: [],
    };
  },
  filters: {
    transState(val) {
      switch (val) {
        case 1:
            return "起草中";
            break;
        case 2:
            return "审核中";
            break;
        case 3:
            return "审核驳回";
            break;
        case 4:
            return "评审中";
            break;
        case 5:
            return "确认中";
            break;
        case 10:
            return "已完成";
            break;
      }
    }
  },
  computed: {
    ...mapGetters(["permissions"])

  },
  watch: {
    showZhong(value, oldval) {
      if (value !== oldval) {
        this.getDataList();
      }
    }
  },
  created() {
    this.getDataList();
  },
  methods: {
      openImport() {
          this.innerVisible = true;
      },
      exportXlsx() {
          this.$message.closeAll();
          this.$message.success("下载成功!");
          window.location.href = "../../static/ryyjkinfo.xlsx";
      },
      // excel表上传
      handleChange(file, fileList) {
          this.fileTemp = file.raw;
          let fileName = file.raw.name;
          let fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
          // 判断上传文件格式
          if (this.fileTemp) {
              if (fileType == "xlsx" || fileType == "xls") {
                  this.importf(this.fileTemp);
                  this.innerVisible = false;

              } else {
                  this.$message({
                      type: "warning",
                      message: "附件格式错误，请删除后重新上传！"
                  });
              }
          } else {
              this.$message({
                  type: "warning",
                  message: "请上传附件！"
              });
          }
      },
      //导入的方法
      importf(obj) {
          this.dialogVisible = true;
          let _this = this;
          let inputDOM = this.$refs.inputer; // 通过DOM取文件数据
          this.file = event.currentTarget.files[0];
          var rABS = false; //是否将文件读取为二进制字符串
          var f = this.file;
          var reader = new FileReader();
          //if (!FileReader.prototype.readAsBinaryString) {
          FileReader.prototype.readAsBinaryString = function(f) {
              var binary = "";
              var rABS = false; //是否将文件读取为二进制字符串
              var pt = this;
              var wb; //读取完成的数据
              var outdata;
              var reader = new FileReader();
              reader.onload = function(e) {
                  var bytes = new Uint8Array(reader.result);
                  var length = bytes.byteLength;
                  for (var i = 0; i < length; i++) {
                      binary += String.fromCharCode(bytes[i]);
                  }
                  var XLSX = require("xlsx");
                  if (rABS) {
                      wb = XLSX.read(btoa(fixdata(binary)), {
                          //手动转化
                          type: "base64"
                      });
                  } else {
                      wb = XLSX.read(binary, {
                          type: "binary"
                      });
                  }
                  // outdata就是你想要的东西 excel导入的数据
                  outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
                  // excel 数据再处理
                  let arr = [];
                  outdata.map(v => {
                      // let jsonString = JSON.stringify(v).replace(/\*/g, '').replace(/\s/ig,'');
                      let jsonString = JSON.stringify(v)
                          .replace(/\//g, "")
                          .replace(/\s/gi, "");
                      //debugger;
                      v = JSON.parse(jsonString);
                      let obj = {};
                      obj.fl = v.分类;
                      obj.yj = v.依据;
                      obj.zbgz = v.指标规则;
                      obj.jkfbz = v.加扣分标准;
                      obj.yjkfl = 'jg'

                      arr.push(obj);
                  });
                  _this.accountList = [...arr];

                  let temp1 = [];
                  _this.accountList.map(it => {
                      temp1.push({
                          fl: it.fl,
                          yj: it.yj,
                          zbgz: it.zbgz,
                          jkfbz: it.jkfbz,
                          yjkfl: it.yjkfl
                      });
                  });

                  addUserImport(temp1).then(r => {
                      console.log(r);
                      if(r.data.data.length != 0){
                          let data = r.data.data;
                          _this.$message.success("导入成功");
                          _this.getDataList();
                      }else{
                          _this.$message.success("导入失败");
                      }

                      //_this.mainDept = [..._this.mainDept, ...peon];
                  }).catch(error => {
                      console.log(error);
                  });
              };
              reader.readAsArrayBuffer(f);
          };
          if (rABS) {
              reader.readAsArrayBuffer(f);
          } else {
              reader.readAsBinaryString(f);
          }
      },
      // 移除excel表
      handleRemove(file, fileList) {
          this.fileTemp = null;
      },
    goSubmitEvaluateList(){
        this.goSpVisible = true;
        if (this.multipleSelection.length == 0) {
            this.$message.warning("请先选择数据");
        }else{
            let qcJlRealName = this.multipleSelection.map(it => {
                return it.qcJlRealName;
            });
            this.showLd = qcJlRealName[0];
        }
    },
    // 查看
    dataView(row) {
      dataDetail(row.id).then(r => {
        this.detailVisible = true;
        this.$nextTick(() => {
          this.$refs.detailNode.init(r.data);
        });
      });
    },


    // 删除数据
    deleteTabData() {
      // 删除只能删除状态为0  1 的数据
      this.$confirm("此操作将删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let ids = this.multipleSelection.map(it => {
            return it.id;
          });
          deleteChoose(ids.toString()).then(r => {
            if (r.data.code == 0) {
              this.$message.success("删除成功");
              this.getDataList();
            } else {
              this.$message.error(r.data.msg);
              this.getDataList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 勾选数据
    handleSelectionChange(val) {


      this.multipleSelection = val;
    },
    // 查询条件的展开关闭
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if (this.searchadvanced) {
        this.searchheight = "auto";
      }
    },
    // 更新数据
    refreashdata(data) {
      if (data == 1) {
        this.getDataList();
      }
    },
    //新增与编辑
    addOrUpdateHandle(obj) {
      this.addOrUpdateVisible = true;
      if (obj) {
        this.dialogTitle = "编辑";
      } else {
        this.dialogTitle = "新增";
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(obj);
      });
    },
    //评价关系
    pingJ(row) {
      this.showZhong = 3;
      this.$nextTick(() => {
        this.$refs.pingjia.init(row);
      });
    },
    //审核关系
    shenH(obj) {
      this.showZhong = 2;
      this.zxshformref = {
        rowid: obj.id
      };
      this.$nextTick(() => {
        this.$refs.zxshform.init(obj);
      });
    },
    //子集返回
    zxpjC(zxpjC) {
      this.showZhong = 1;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
        userList(
        Object.assign(
          {
              yjkfl:'jg',
              current: this.pageIndex, //当前页
              size: this.pageSize // 每页条数

          },
          this.searchForm
        )
      )
        .then(response => {
          this.dataList = response.data.data.records;
          this.totalPage = response.data.data.total;
          this.dataListLoading = false;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
    },
    // 关闭弹窗
    closeDialog() {
      this.addOrUpdateVisible = false;
    },
    closeDialog1() {
      this.detailVisible = false;
    },
    // 显示用户
    showUser(taskId) {
      this.taskId = taskId;
      this.usercommentTask = true;
      this.$nextTick(() => {
        this.$refs.userTask.init("", "zxshr", JSON.stringify([]), "htcj");
      });
    },
    // 提交任务
    submitfyTask(data) {
      if (data.tableDataUser.length != 1) {
        this.$message.closeAll();
        this.$message.error("请指定唯一的审批人");
      } else {
        changeAss({
          taskId: this.taskId,
          userId: data.tableDataUser[0].userId
        }).then(res => {
          this.$message.closeAll();
          this.$message.success("审批人更换成功");
          this.showPicDialog = false;
          this.getDataList();
        });
      }
    },
    // 显示信息
    showMessage(proId) {
      showMessage(proId).then(res => {
        this.hisTableData = res.data.data;
      });
      this.actPicUrl = `/act/sp/view/` + proId;
      this.actPicUrlList = [];
      this.actPicUrlList.push(`/act/sp/view/` + proId);
      this.showPicDialog = true;
    },
    indexMethod(index) { //修改table表索引
        return (this.pageIndex-1)*this.pageSize+index+1;
    }
    // 表格中state对应状态，前面是真实状态，后面为页面展示状态
    // 0 待创建审核关系    起草中
    // 1 待创建评价关系    起草中
    // 2 专项工作填报中  	待提交
    // 3 专项工作审核中    已提交
    // 5 专项工作审核驳回   审核驳回
    // 6 专项工作确认中     工作确认
    // 7 专项工作填报中     履约中
    // 8 专项工作评分中      评分中
    // 80 待评分            待评分
    // 81 评分完成          评分完成
    // 82 驳回后重新评分     评分退回
    // 9  专项工作结果汇总    结果汇总----提交审批  0r----结束流程
    // 10 专项工作考核员审核    待评审----结束流程
    // 11 流程结束      流程结束
  },

};
</script>

<style lang="scss" scoped>
  /deep/.el-table__header-wrapper .el-checkbox {
    display: none;
  }
  .el-table__header-wrapper .el-checkbox {
    display: none;
  }

  /deep/.el-icon-download:before{font-size: 30px!important}
  /deep/.el-divider__text, .el-link {font-size: 14px!important;}
  /deep/.el-link [class*=el-icon-]+span{margin-left: 0px!important;}
  /deep/.el-button+.el-button{margin-right: 10px!important;}
</style>
